﻿//lamber 光照模型
Shader "Demon/lamber/lamber_change"
{
	Properties
	{
		[Header(step)]
		[Toggle]_USESTEP("use step",int) = 1
		_Vlaue("Value",Range(0,1)) = 0.5

		[Header(smoothstep)]
		[Toggle]_USESMOOTHSTEP("use smoothstep",int) = 1
		_RampThreshold("RampThreshold",Range(0,1)) = 0.8		//明暗边界阀值
		_RampSmooth("RampSmooth",Range(0,1)) = 0.1				//明暗边界平滑过渡
		_HColor("Highlight Color",Color) = (1,1,1,1)   //亮部颜色
		_SColor("shadow Color",Color) = (0.5,0.5,0.5,1)  //阴影部分颜色

		[Header(rampMap)]
		[Toggle]_USERAMPMAP("use rampMap",int) = 1
		_RampMap ("RampMap", 2D) = "white" {}
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			// make fog work
			#pragma multi_compile_fog

			#pragma multi_compile _ _USESTEP_ON
			#pragma multi_compile _ _USESMOOTHSTEP_ON
			#pragma multi_compile _ _USERAMPMAP_ON

			
			#include "UnityCG.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
				float3 normal :NORMAL;
			};

			struct v2f
			{
				float2 uv : TEXCOORD0;
				UNITY_FOG_COORDS(1)
				float4 vertex : SV_POSITION;
				float3 worldNormal :TEXCOORD1;
				float3 worldPos : TEXCOORD2;
			};

			sampler2D _RampMap;
			float4 _RampMap_ST;
			float _Vlaue;

			float _RampThreshold;
			float _RampSmooth;
			float4 _HColor;
			float4 _SColor;
			
			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = TRANSFORM_TEX(v.uv, _RampMap);
				o.worldNormal = UnityObjectToWorldNormal(v.normal);
				o.worldPos = mul(unity_ObjectToWorld,v.vertex); 
				UNITY_TRANSFER_FOG(o,o.vertex);
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				fixed4 col = 0;
				float3 L = normalize(_WorldSpaceLightPos0.xyz);   
				float3 N = normalize(i.worldNormal);
				float NdotL = dot(N,L);
				NdotL = NdotL * 0.5 + 0.5;
				col = NdotL;

				#if _USESTEP_ON 
					col = step(_Vlaue,NdotL);
				#endif

				#if _USESMOOTHSTEP_ON 
					col = smoothstep(_RampThreshold - _RampSmooth * 0.5,_RampThreshold + _RampSmooth * 0.5,NdotL);
					col = lerp(_SColor,_HColor,col);
				#endif

				#if _USERAMPMAP_ON
					col = tex2D(_RampMap,fixed2(NdotL,NdotL));
				#endif

				return col;
			}
			ENDCG
		}
	}
	FallBack "Diffuse"
}
